package com.archermind.style.controller;

import com.alibaba.fastjson.JSONObject;
import com.archermind.style.bean.User;
import com.archermind.style.interceptor.Auth;
import com.archermind.style.service.OperationService;
import com.archermind.style.service.UserService;
import com.archermind.style.util.Result;
import com.archermind.style.util.Search;
import com.archermind.style.util.SessionUtil;
import com.feng.fengDUtil.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 *@Author 丰建立
 *@Date 2019/1/25 16:43
 * 用户
 */
@Auth("用户")
@RestController
@RequestMapping(value = "/sys")
public class SysAction {
    @Autowired
    private UserService userService;
    @Autowired
    private OperationService opService;


    @RequestMapping(value = "/listUser")
    public Result listUser(Page page, Search search) {
        userService.listUser(page,search);
        return Result.success(page);
    }

    @RequestMapping(value = "/listRole")
    public Result listRole(Page page, Search search) {
        userService.listRole(page,search);
        return Result.success(page);
    }

    @RequestMapping(value="/delUser")
    public Result delUser(String ids){
        try {
            userService.deleteByIds(User.class,ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/checkUniqueName_mute")
    public JSONObject checkUniqueName(@RequestParam(value = "id",required = false) Integer id,
                                      @RequestParam(value = "account") String account)
    {
        JSONObject ret = new JSONObject();
        ret.put("isUnique",userService.isUnique(account,id));
        return ret;
    }

    @RequestMapping(value="/saveUser")
    public Result saveUser(@RequestBody User user){
        try {
            if(user.getId()==null){
                user.setCreator(SessionUtil.getCurUserId());
                user.setCreateTime(new Date());
                opService.saveLog("添加了用户："+user.getAccount());
            }else{
                user.setModifier(SessionUtil.getCurUserId());
                user.setModifyTime(new Date());
                opService.saveLog("编辑了用户："+user.getAccount());
            }
            userService.saveOrUpdate(user);
            return Result.success(user);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}
